package com.elean.pms.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

/**
 * @Auther: 周作为
 * @Date: 2020/3/2 13:55
 * @Description:
 */
@Component
public class JedisUtil {

    @Autowired
    private JedisPool jedisPool;


    /**
     * 存储REDIS队列 顺序存储
     * @param  key redis键名
     * @param  value 键值
     */
    public  void lpush(byte[] key, byte[] value) {

        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.lpush(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //释放redis对象
            jedis.close();
        }
    }

    /**
     * 存储REDIS队列 反向存储
     * @param  key redis键名
     * @param  value 键值
     */
    public  void rpush(byte[] key, byte[] value) {

        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.rpush(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //释放redis对象
            jedis.close();
        }
    }

    /**
     * 获取队列数据
     * @param  key 键名
     * @return
     */
    public byte[] rpop(byte[] key) {

        byte[] bytes = null;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            bytes = jedis.rpop(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //释放redis对象
           jedis.close();
        }
        return bytes;
    }

    /**
     * 获取队列数据
     * @param  key 键名
     * @return
     */
    public byte[] lpop(byte[] key) {

        byte[] bytes = null;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            bytes = jedis.lpop(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //释放redis对象
            jedis.close();
        }
        return bytes;
    }


}
